﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using System.IO;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Framework.Controls;

using PlanningGIS.CoordCovert.model;
using DevComponents.DotNetBar;
using PlanningGIS.Engine.Controls;
using PlanningGIS.ArcGIS.Custom;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Util.Convertor;

namespace PlanningGIS.CoordCovert.form
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmAffine6Para : Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        private string folder = AppConfig.SysFolder + "\\parameter";

        /// <summary>
        /// 
        /// </summary>
        private const string DEFAULT_FILE_NAME = "param6_def.txt";

        /// <summary>
        /// 
        /// </summary>
        public FrmAffine6Para()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAffine6Para_Load(object sender, EventArgs e)
        {
            LoadDefault();
            prgorAll = new Progressor() { ProgressBar = progressBarAll };
            prgorSub = new Progressor() { ProgressBar = progressBarSub };

        }

        /// <summary>
        /// 总进度条
        /// </summary>
        private Progressor prgorAll = null;

        /// <summary>
        /// 分进度条
        /// </summary>
        private Progressor prgorSub = null;

        /// <summary>
        /// 加载默认参数
        /// </summary>
        private void LoadDefault()
        {
            if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
            string fileName = folder + "\\" + DEFAULT_FILE_NAME;
            if (!File.Exists(fileName)) File.Create(fileName);
            LoadPara(fileName);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            IList<IFeatureClass> fclasslist = new List<IFeatureClass>();
            OpenDataDialog addData = new OpenDataDialog();
            if (addData.ShowDialog(this) != System.Windows.Forms.DialogResult.OK) return;
            try
            {
                List<IDataNode> datanodes = addData.GetSelectNode<IDataNode>();
                foreach (IDataNode datanode in datanodes)
                {
                    if (datanode is FeatureClassNode)
                    {
                        fclasslist.Add(datanode.Open() as IFeatureClass);
                    }
                    else if (datanode is DatasetNode)
                    {
                        IEnumDataset datasetenum = datanode.Open().Subsets;
                        IDataset dataset = datasetenum.Next();
                        while (dataset != null)
                        {
                            fclasslist.Add(dataset as IFeatureClass);
                            dataset = datasetenum.Next();
                        }
                    }
                }
                foreach (IFeatureClass featureClass in fclasslist)
                {
                    ListViewItem pListViewItem = new ListViewItem(((IDataset)featureClass).BrowseName);
                    pListViewItem.Tag = featureClass;
                    pListViewItem.Checked = true;
                    this.lstLayers.Items.Add(pListViewItem);
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, ex.Message);
            }
        }

        /// <summary>
        /// 移除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.lstLayers.Items.Count == 0) return;
            if (this.lstLayers.SelectedItems.Count == 0) return;
            try
            {
                foreach (ListViewItem pListViewItem in this.lstLayers.SelectedItems)
                {
                    this.lstLayers.Items.Remove(pListViewItem);
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, "移除失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 导入参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "导入参数文件";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "参数文件(*.txt)|*.txt";
            openFileDialog1.InitialDirectory = folder;
            if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            LoadPara(openFileDialog1.FileName);
        }

        /// <summary>
        /// 导入参数文件
        /// </summary>
        /// <param name="filename"></param>
        private void LoadPara(string filename)
        {
            StreamReader streamReaderr = null;
            double tmp = 0;
            try
            {
                streamReaderr = File.OpenText(filename);

                string txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt, out tmp))
                {
                    textBox_X0.Value = tmp;
                }
                txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt, out tmp))
                {
                    textBox_Y0.Value = tmp;
                }
                txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt, out tmp))
                {
                    textBox_yy0.Value = tmp;
                }
                txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt,out tmp))
                {
                    textBox_xx0.Value = tmp;
                }
                txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt, out tmp))
                {
                    textBox_c.Value = tmp;
                }
                txt = streamReaderr.ReadLine();
                if (!string.IsNullOrWhiteSpace(txt) && double.TryParse(txt, out tmp))
                {
                    textBox_d.Value = tmp;
                }   
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, "导入失败：" + ex.Message);
            }
            finally
            {
                if (streamReaderr != null) streamReaderr.Close();
                streamReaderr = null;
            }
        }

        /// <summary>
        /// 导出参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Title = "导出参数文件";
            saveFileDialog1.Filter = "参数文件(*.txt)|*.txt";
            saveFileDialog1.InitialDirectory = folder;
            if (saveFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            string strTextpath = saveFileDialog1.FileName;
            try
            {                
                if (File.Exists(strTextpath)) File.Delete(strTextpath);
                StreamWriter sw = File.CreateText(strTextpath);
                sw.WriteLine(textBox_X0.Value);
                sw.WriteLine(textBox_Y0.Value);
                sw.WriteLine(textBox_yy0.Value);
                sw.WriteLine(textBox_xx0.Value);
                sw.WriteLine(textBox_c.Value);
                sw.WriteLine(textBox_d.Value);
                sw.Close();
                MsgHelper.ShowInfomationMsgbox(this, "导出成功！");
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, "导出失败：" + ex.Message);
            }
        }
    
        /// <summary>
        /// 开始转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (textBox_X0.Text.Trim() == "" || textBox_Y0.Text.Trim() == "" || textBox_yy0.Text.Trim() == "" ||
                textBox_xx0.Text.Trim() == "" | textBox_c.Text.Trim() == "" || textBox_d.Text.Trim() == "")
            {
                MsgHelper.ShowWarningMsgbox(this, "请设置参数！");
                return;
            }
            List<IFeatureClass> pFeatClses = new List<IFeatureClass>();
            foreach (ListViewItem pListViewItem in this.lstLayers.Items)
            {
                if (pListViewItem.Checked && pListViewItem.Tag != null && pListViewItem.Tag is IFeatureClass)
                {
                    pFeatClses.Add(pListViewItem.Tag as IFeatureClass);
                }
            }
            if (pFeatClses.Count == 0)
            {
                MsgHelper.ShowWarningMsgbox(this, "请设置需要转换的数据！");
                return;
            }
            AffineConvert6Parameters affineConvert6Parameters = new AffineConvert6Parameters();
            affineConvert6Parameters.X0 = textBox_X0.Value;
            affineConvert6Parameters.Y0 = textBox_Y0.Value;
            affineConvert6Parameters.x0 = textBox_xx0.Value;
            affineConvert6Parameters.y0 = textBox_yy0.Value;
            affineConvert6Parameters.c = textBox_c.Value;
            affineConvert6Parameters.d = textBox_d.Value;

            affineConvert6Parameters.FeatClses = pFeatClses;
            affineConvert6Parameters.SetProcessBarMax = prgorAll.SetMaxValue;
            affineConvert6Parameters.SetProcessBarCurrent = prgorAll.SetCurrentValue;// setAllPgbCurrent;
            affineConvert6Parameters.SetSubProcessBarMax = prgorSub.SetMaxValue;// setSubPgbMax;
            affineConvert6Parameters.SetSubProcessBarCurrent = prgorSub.SetCurrentValue;// setSubPgbCurrent;
            btnConvert.Enabled = false;
            btnCancel.Enabled = false;
            FrameworkHelper.ShowLogPanel();
            if (affineConvert6Parameters.Operate())
            {
                MsgHelper.ShowInfomationMsgbox(this, "转换完毕！");
            }
            else
            {
                MsgHelper.ShowErrorMsgbox(this, "转换失败，具体信息请参看日志！");
            }
            btnConvert.Enabled = true;
            btnCancel.Enabled = true;
            prgorAll.Stop();
            prgorSub.Stop();
            //progressBarAll.Value = 0;
            //progressBarAll.Text = "";
            //progressBarSub.Value = 0;
            //progressBarSub.Text = "";
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
